﻿using System;
using System.Data;
using System.IO;

namespace WinFormClient
{
    public static class Tools
    {
        private static readonly Object locked = new Object();

        public static string ParameterConvert(string mapsClassName)
        {
            return mapsClassName.Substring(0, 1).ToLower() + mapsClassName.Substring(1);
        }

        public static string FieldConvert(string fieldName)
        {
            return "_" + fieldName.Substring(0, 1).ToLower() + fieldName.Substring(1);
        }

        public static string PropertyConvert(string fieldName)
        {
            return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
        }

        public static string ClassConvert(string tabName)
        {
            string[] strs = tabName.Split('_');
            string rtn = "";
            foreach (string s in strs)
            {
                rtn += s.Substring(0,1).ToUpper() + s.Substring(1).ToLower();
            }

            return rtn;
        }

        public static void TextSaveToFile(string fileContent, string folder, string fileName)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\Result\" + folder;
            if (!System.IO.Directory.Exists(path))
            {
                lock (locked)
                {
                    if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
                }
            }

            System.IO.File.WriteAllText(path + @"\" + fileName, fileContent, System.Text.Encoding.UTF8);
        }

        public static void RewriteTextFile(string virtualPath,string rewritePath, string searchText, string replaceSearchText)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + virtualPath;
            string rpath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + rewritePath;

            if (System.IO.File.Exists(path))
            {
                string str = System.IO.File.ReadAllText(path, System.Text.Encoding.UTF8);
                if (!String.IsNullOrEmpty(searchText)) str = str.Replace(searchText, replaceSearchText);

                if (!Directory.Exists(Path.GetDirectoryName(rpath))) Directory.CreateDirectory(Path.GetDirectoryName(rpath));
                System.IO.File.WriteAllText(rpath, str, System.Text.Encoding.UTF8);
            }
        }

        public static void WriteContentToTextFile(string content, string rewritePath, string searchText, string replaceSearchText)
        {
            string rpath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + rewritePath;
            
            if (!Directory.Exists(Path.GetDirectoryName(rpath))) Directory.CreateDirectory(Path.GetDirectoryName(rpath));
            System.IO.File.WriteAllText(rpath, ((String.IsNullOrEmpty(searchText))?content:content.Replace(searchText,replaceSearchText)), System.Text.Encoding.UTF8);
        }

        public static void SaveFile(Stream str, string folder)
        {
            string rpath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + folder;
            
            if (!Directory.Exists(Path.GetDirectoryName(rpath))) Directory.CreateDirectory(Path.GetDirectoryName(rpath));

            FileStream sf = new FileStream(rpath, FileMode.OpenOrCreate);
            byte[] buffer = new byte[(int)str.Length];
            str.Read(buffer, 0, (int)str.Length);
            sf.Write(buffer, 0, buffer.Length);
            sf.Flush();
            sf.Close();
            str.Dispose();
        }
    }
}
